package com.ebupt.migu.music.common.entity;

import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author liuzz
 */
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class ResultObject<T> {

    /**
     * 返回编码
     **/
    private String code = "0000";
    /**
     * 返回信息
     **/
    private String msg;
    /**
     * 数据
     */
    private T data;

    /**
     * to Json
     *
     * @return
     */
    public String toJson() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    public ResultObject success(){
        return new ResultObject();
    }

    public ResultObject success(String msg){
        ResultObject res = new ResultObject();
        res.setMsg(msg);
        return res;
    }

    public ResultObject success(String msg,Object data){
        ResultObject res = new ResultObject();
        res.setMsg(msg);
        res.setData(data);
        return res;
    }

    public static<T> ResultObject<T> ok(String msg){
        ResultObject<T> r = new ResultObject<T>();
        r.setCode("0000");
        r.setMsg(msg);
        return r;
    }

    public static<T> ResultObject<T> error(String msg) {
        ResultObject<T> r = new ResultObject<T>();
        r.setCode("500");
        r.setMsg(msg);
        return r;
    }

    public static<T> ResultObject<T> ok(T data) {
        ResultObject<T> r = new ResultObject<T>();
        r.setCode("0000");
        r.setData(data);
        return r;
    }
}
